package stack;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * 推荐结果打散
 */
public class RecommendResult {

    public static void main(String[] args) {
        List<String> picVideos = new ArrayList<>();
        picVideos.add("v_0");
        picVideos.add("p_3");
        picVideos.add("v_1");
        picVideos.add("v_2");
        picVideos.add("p_4");
        picVideos.add("p_5");
        picVideos.add("v_6");
        picVideos.add("p_7");
        picVideos.add("v_8");
        picVideos.add("p_9");
        picVideos.add("p_10");
        picVideos.add("v_11");
//        picVideos.add("v_12");
//        picVideos.add("v_13");
        picVideos.add("p_14");
//        picVideos.add("v_15");
//        picVideos.add("v_16");
        List<String> result = new RecommendResult().recommendResult(picVideos, 3);
        for (int i = 0; i < result.size(); i++) {
            System.out.println(result.get(i));
        }
    }

    public List<String> recommendResult(List<String> picVideos, int maxInterval) {
        List<String> result = new ArrayList<>();
        if (picVideos == null || picVideos.size() == 0) {
            return result;
        }
        Queue<String> videoQueue = new LinkedList<>();
        Queue<String> picQueue = new LinkedList<>();
        int index = 0;
        int size = picVideos.size();

        while (index < size) {
            if (isVideo(picVideos.get(index))) {
                videoQueue.add(picVideos.get(index));
            } else {
                picQueue.add(picVideos.get(index));
            }
            index++;
        }

        int currentSize = 0;
        while (!videoQueue.isEmpty() && !picQueue.isEmpty()) {
            if (currentSize >= maxInterval) {
                result.add(picQueue.poll());
                currentSize = 0;
            } else {
                result.add(videoQueue.poll());
                currentSize++;
            }
        }
        while (!videoQueue.isEmpty()){
            result.add(videoQueue.poll());
        }
        if (!picQueue.isEmpty() && currentSize >= maxInterval) {
            result.add(picQueue.poll());
        }
        return result;
    }

    private boolean isVideo(String str) {
        return str.contains("v");
    }

}
